<?php
/**
 * Class that operate on table 'jobs_applied'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class JobsAppliedMySqlDAO implements JobsAppliedDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return JobsAppliedMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM jobs_applied WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM jobs_applied';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM jobs_applied ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param jobsApplied primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM jobs_applied WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param JobsAppliedMySql jobsApplied
 	 */
	public function insert($jobsApplied){
		$sql = 'INSERT INTO jobs_applied (job_id, email, subject, message, resume, created_by, create_date, updated_by, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($jobsApplied->jobId);
		$sqlQuery->set($jobsApplied->email);
		$sqlQuery->set($jobsApplied->subject);
		$sqlQuery->set($jobsApplied->message);
		$sqlQuery->set($jobsApplied->resume);
		$sqlQuery->setNumber($jobsApplied->createdBy);
		$sqlQuery->set($jobsApplied->createDate);
		$sqlQuery->setNumber($jobsApplied->updatedBy);
		$sqlQuery->set($jobsApplied->updated_date);
		$sqlQuery->setNumber($jobsApplied->status);

		$id = $this->executeInsert($sqlQuery);	
		$jobsApplied->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param JobsAppliedMySql jobsApplied
 	 */
	public function update($jobsApplied){
		$sql = 'UPDATE jobs_applied SET job_id = ?, email = ?, subject = ?, message = ?, resume = ?, created_by = ?, create_date = ?, updated_by = ?, updated_date = ?, status = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($jobsApplied->jobId);
		$sqlQuery->set($jobsApplied->email);
		$sqlQuery->set($jobsApplied->subject);
		$sqlQuery->set($jobsApplied->message);
		$sqlQuery->set($jobsApplied->resume);
		$sqlQuery->setNumber($jobsApplied->createdBy);
		$sqlQuery->set($jobsApplied->createDate);
		$sqlQuery->setNumber($jobsApplied->updatedBy);
		$sqlQuery->set($jobsApplied->updated_date);
		$sqlQuery->setNumber($jobsApplied->status);

		$sqlQuery->setNumber($jobsApplied->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM jobs_applied';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByJobId($value){
		$sql = 'SELECT * FROM jobs_applied WHERE job_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM jobs_applied WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySubject($value){
		$sql = 'SELECT * FROM jobs_applied WHERE subject = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMessage($value){
		$sql = 'SELECT * FROM jobs_applied WHERE message = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByResume($value){
		$sql = 'SELECT * FROM jobs_applied WHERE resume = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM jobs_applied WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreateDate($value){
		$sql = 'SELECT * FROM jobs_applied WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedBy($value){
		$sql = 'SELECT * FROM jobs_applied WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM jobs_applied WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM jobs_applied WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByJobId($value){
		$sql = 'DELETE FROM jobs_applied WHERE job_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM jobs_applied WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySubject($value){
		$sql = 'DELETE FROM jobs_applied WHERE subject = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMessage($value){
		$sql = 'DELETE FROM jobs_applied WHERE message = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByResume($value){
		$sql = 'DELETE FROM jobs_applied WHERE resume = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM jobs_applied WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreateDate($value){
		$sql = 'DELETE FROM jobs_applied WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedBy($value){
		$sql = 'DELETE FROM jobs_applied WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM jobs_applied WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM jobs_applied WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return JobsAppliedMySql 
	 */
	protected function readRow($row){
		$jobsApplied = new JobsApplied();
		
		$jobsApplied->id = $row['id'];
		$jobsApplied->jobId = $row['job_id'];
		$jobsApplied->email = $row['email'];
		$jobsApplied->subject = $row['subject'];
		$jobsApplied->message = $row['message'];
		$jobsApplied->resume = $row['resume'];
		$jobsApplied->createdBy = $row['created_by'];
		$jobsApplied->createDate = $row['create_date'];
		$jobsApplied->updatedBy = $row['updated_by'];
		$jobsApplied->updated_date = $row['updated_date'];
		$jobsApplied->status = $row['status'];

		return $jobsApplied;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return JobsAppliedMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>